<template>
  <div class="mylist">
    <div class="todo">
      <h3
        :style="{ background: $store.state.backcolor }"
        @click="toggle('todo')"
      >
        未完成
        <span v-if="todoarrow">&or;</span>
        <span v-else>&gt;</span>
      </h3>
      <transition name="slidedown">
        <ul v-show="todoarrow">
          <li v-for="item in list" :key="item.id">
            <div class="todolist" v-show="item.code === 0">
              <div class="left">
                <input
                  type="checkbox"
                  @click="changestate(item, 1)"
                  v-model="item.check"
                />
                <span>{{ item.content }}</span>
              </div>
              <button @click="changestate(item, 2)">取消</button>
            </div>
          </li>
        </ul>
      </transition>
    </div>
    <div class="done">
      <h3
        :style="{ background: $store.state.backcolor }"
        @click="toggle('done')"
      >
        已完成
        <span v-if="donearrow">&or;</span>
        <span v-else>&gt;</span>
      </h3>

      <transition name="slidedown">
        <ul v-show="donearrow">
          <li v-for="item in list" :key="item.id">
            <div class="todolist" v-show="item.code === 1">
              <div class="left">
                <input
                  type="checkbox"
                  v-model="item.check"
                  @click="changestate(item, 0)"
                />
                <span>{{ item.content }}</span>
              </div>
              <span>{{ new Date() | formater }}</span>
            </div>
          </li>
        </ul>
      </transition>
    </div>
    <div class="cancel">
      <h3
        :style="{ background: $store.state.backcolor }"
        @click="toggle('cel')"
      >
        已取消
        <span v-if="celarrow">&or;</span>
        <span v-else>&gt;</span>
      </h3>

      <transition name="slidedown">
        <ul v-show="celarrow">
          <li v-for="item in list" :key="item.id">
            <div class="todolist" v-show="item.code === 2">
              <div class="left">
                <span>{{ item.content }}</span>
              </div>
              <button @click="changestate(item, 0)">恢复</button>
            </div>
          </li>
        </ul>
      </transition>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "mylist",
  data() {
    return {};
  },
  computed: {
    ...mapState(["todoarrow", "donearrow", "celarrow", "list"]),
  },
  methods: {
    toggle(type) {
      switch (type) {
        case "todo":
          this.$store.commit("changetodo", { type: "init" });
          break;
        case "done":
          this.$store.commit("changedone", { type: "init" });
          break;
        case "cel":
          this.$store.commit("changecel", { type: "init" });
          break;
      }
    },
    changestate(item, i) {
      item.code = i;
      if (i === 0) {
        item.check = false;
        this.$store.commit("changetodo", { type: "todo" });
      } else if (i === 1) {
        item.check = true;
        this.$store.commit("changedone", { type: "done" });
      } else if (i === 2) {
        this.$store.commit("changecel", { type: "cancel" });
      }
    },
  },
  filters: {
    formater(val) {
      let date = new Date(val);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      return `${year}-${month}-${day}`;
    },
  },
};
</script>

<style lang="scss" scoped>
.mylist {
  width: 100%;
  h3 {
    color: #fff;
    font-size: 20px;
    height: 40px;
    padding: 0 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .todo,
  .done,
  .cancel {
    width: 100%;
    padding: 0 10px;
    ul {
      width: 100%;
      height: 200px;
      border: 2px solid red;
      overflow: auto;
      li {
        div {
          display: flex;
          justify-content: space-between;
          align-items: center;
          height: 30px;
          padding: 5px;
          .left {
            input {
              width: 20px;
              height: 20px;
              vertical-align: middle;
            }
            span {
              vertical-align: middle;
            }
          }
          button {
            padding: 0 4px;
          }
        }
      }
    }
    .slidedown-enter,
    .slidedown-leave-to {
      height: 0;
      opacity: 0;
    }
    .slidedown-enter-to,
    .slidedown-leave {
      height: 200px;
      opacity: 1;
    }
    .slidedown-enter-active,
    .slidedown-leave-active {
      transition: all 0.5s linear;
    }
  }
}
</style>